package com.heima.wemedia.feign;

import com.alibaba.fastjson.JSON;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmChannelPageReqDto;
import com.heima.model.wemedia.dtos.WmSensitivePageReqDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmSensitiveservice;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/api/v1/feign")
public class WemediaClient implements IWemediaClient {

    @Autowired
    private WmSensitiveservice wmSensitiveservice;
    @Autowired
    private WmChannelService wmChannelService;
    @Autowired
    private WmUserService wmUserService;
    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 自媒体文章人工审核成功
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_pass")
    @Override
    public ResponseResult authPassNews(@RequestBody NewsAuthDto dto) {
        return wmNewsService.authPassNews(dto);
    }

    /**
     * 自媒体文章人工审核失败
     * @param dto
     * @return
     */
    @PostMapping("/news/auth_fail")
    @Override
    public ResponseResult authFailNews(@RequestBody NewsAuthDto dto) {
        return wmNewsService.authFailNews(dto);
    }

    /**
     * 查询文章详情
     * @param id
     * @return
     */
    @GetMapping("/news/one_vo/{id}")
    @Override
    public ResponseResult findNewsOne(@PathVariable Integer id) {
        return wmNewsService.findWmNewsVoById(id);
    }

    /**
     * 查询文章列表
     * @param dto
     * @return
     */
    @PostMapping("/news/list_vo")
    @Override
    public PageResponseResult findNewsList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }

    /**
     * 创建自媒体账号
     * @param apUser
     */
    @PostMapping("/wmuser/save")
    @Override
    public void createWmUser(@RequestBody String apUser) {
        wmUserService.create(apUser);
    }

    /**
     * 频道修改
     * @param wmChannel
     */
    @PostMapping("/channel/update")
    @Override
    public ResponseResult updateChannel(@RequestBody WmChannel wmChannel) {
        ResponseResult result = wmChannelService.updateOrSave(wmChannel);
        return result;
    }

    /**
     * 频道保存
     * @param wmChannel
     */
    @PostMapping("/channel/save")
    @Override
    public ResponseResult saveChannel(@RequestBody WmChannel wmChannel) {
        ResponseResult result = wmChannelService.updateOrSave(wmChannel);
        return result;
    }

    /**
     * 查询频道列表
     * @param dto
     * @return
     */
    @PostMapping("/channel/list")
    @Override
    public PageResponseResult findChannelList(@RequestBody WmChannelPageReqDto dto) {
        return wmChannelService.findList(dto);
    }

    /**
     * 查询单条频道
     * @param id
     * @return
     */
    @GetMapping("/channel/one/{id}")
    @Override
    public String findChannelOne(@PathVariable Integer id) {
        return JSON.toJSONString(wmChannelService.getById(id));
    }

    /**
     * 删除频道
     * @param id
     */
    @GetMapping("/channel/del/{id}")
    @Override
    public ResponseResult delChannel(@PathVariable Integer id) {
        ResponseResult result = wmChannelService.delChannel(id);
        return result;
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     */
    @PostMapping("/sensitive/update")
    @Override
    public void updateSensitive(@RequestBody WmSensitive wmSensitive) {
        wmSensitiveservice.updateOrSave(wmSensitive);
    }

    /**
     * 保存敏感词
     * @param wmSensitive
     */
    @PostMapping("/sensitive/save")
    @Override
    public void saveSensitive(@RequestBody WmSensitive wmSensitive) {
        wmSensitiveservice.updateOrSave(wmSensitive);
    }

    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @PostMapping("/sensitive/list")
    @Override
    public PageResponseResult findSensitiveList(@RequestBody WmSensitivePageReqDto dto) {
        return wmSensitiveservice.findList(dto);
    }

    /**
     * 查询单条敏感词
     * @param id
     * @return
     */
    @GetMapping("/sensitive/one/{id}")
    @Override
    public String findSensitiveOne(@PathVariable Integer id) {
        return JSON.toJSONString(wmSensitiveservice.getById(id));
    }

    /**
     * 删除敏感词
     */
    @DeleteMapping("/sensitive/del/{id}")
    @Override
    public void delSensitive(@PathVariable Integer id) {
        if(id == null){
            log.error("WemediaClient--id为空");
        }
        wmSensitiveservice.delSensitive(id);
    }
}
